package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.servlet.util.Validation;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;

@SuppressWarnings("serial")
public class EditPhotoController extends HttpServlet {

	private static Logger log = Logger.getLogger(EditPhotoController.class);

	@EJB
	private PhotoDaoLocal photoDao;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			@SuppressWarnings("unchecked")
			List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

			String idString = null;

			for (FileItem item : items) {
				if (item.isFormField() && item.getFieldName().equals("id"))
					idString = item.getString();
			}

			//String idString = request.getParameter("id");
			Integer id = Integer.parseInt(idString);

			String title = null;
			String description = null;
			boolean publicVisible = false;
			boolean tagsAllowed = false;
			boolean notesAllowed = false;
			boolean commentsAllowed = false;

			Photo photo = photoDao.findById(id);

			for (FileItem item : items) {
				if (item.isFormField()) {

					boolean isValid = true;

					if(item.getFieldName().equals("title")) {
						title = item.getString();
						isValid = Validation.validateString(title);

						if(isValid)
							photo.setTitle(title);

					} else if(item.getFieldName().equals("description")) {
						description = item.getString();
						isValid = Validation.validateString(description);

						if(isValid)
							photo.setDescription(description);

					} else if(item.getFieldName().equals("public")) {
						publicVisible = item.getString() != null;	// ako nije štiklirano biće true
						photo.setPubliclyAvailable(publicVisible);

					} else if(item.getFieldName().equals("tags_allowed")) {
						tagsAllowed = item.getString() != null;
						photo.setTagsAllowed(tagsAllowed);

					} else if(item.getFieldName().equals("notes_allowed")) {
						notesAllowed = item.getString() != null;
						photo.setNotesAllowed(notesAllowed);

					} else if(item.getFieldName().equals("comments_allowed")) {
						commentsAllowed = item.getString() != null;
						photo.setCommentsAllowed(commentsAllowed);

					}

					if(!isValid) {
						log.error(new Date() + "Greška pri validaciji parametara, ili nije selektovan album.");
						request.setAttribute("photo", photo);
						getServletContext().getRequestDispatcher("/viewPhoto.jsp").forward(request, response);
					}

				} 
			}


			photoDao.merge(photo);

			request.setAttribute("photo", photo);
			getServletContext().getRequestDispatcher("/viewPhoto.jsp").forward(request, response);
			return;

		} catch (FileUploadException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
